package com.morningstar.flow.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.morningstar.common.arco.SelectOption;
import com.morningstar.common.dao.entity.CommonQuery;
import com.morningstar.common.result.Result;
import com.morningstar.common.result.ServiceCheckResult;
import com.morningstar.flow.dao.entity.FlowProcessGroup;
import com.morningstar.flow.service.FlowProcessGroupService;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

import static com.morningstar.common.constants.permission.PermissionConstants.*;

/**
 * 流程组
 *
 * @author yanglianshan
 */
@Slf4j
@RestController
@RequestMapping("/flow/process/group")
public class FlowProcessGroupController {

    private static final String PERMISSION = "flow.process.group";
    private static final String PERMISSION_LIST = PERMISSION + LIST;
    private static final String PERMISSION_DETAIL = PERMISSION + DETAIL;
    private static final String PERMISSION_UPDATE = PERMISSION + UPDATE;
    private static final String PERMISSION_ADD = PERMISSION + ADD;
    private static final String PERMISSION_DELETE = PERMISSION + DELETE;

    @Resource
    private FlowProcessGroupService processGroupService;


    @PostMapping(value = "/list")
    @SaCheckPermission(PERMISSION_LIST)
    public Result<List<FlowProcessGroup>> list(@RequestBody CommonQuery<FlowProcessGroup, FlowProcessGroup> commonQuery) {
        FlowProcessGroup query = commonQuery.getQuery();
        LambdaQueryWrapper<FlowProcessGroup> queryWrapper = new LambdaQueryWrapper<>();
        if (query != null) {
            queryWrapper.like(StringUtils.isNotBlank(query.getProcessGroupName()), FlowProcessGroup::getProcessGroupName, query.getProcessGroupName());
        }
        queryWrapper.orderByAsc(FlowProcessGroup::getSort);
        queryWrapper.orderByDesc(FlowProcessGroup::getCreateTime);
        return Result.successData(processGroupService.list(queryWrapper));
    }

    @PostMapping
    @SaCheckPermission(PERMISSION_ADD)
    public Result<Object> add(@RequestBody FlowProcessGroup flowProcessGroup) {
        ServiceCheckResult checkResult = processGroupService.checkAddAndUpdate(flowProcessGroup);
        if (checkResult.isResult()) {
            return Result.fail(checkResult.getMessage());
        }
        processGroupService.save(flowProcessGroup);
        return Result.successNoData();
    }

    @PostMapping("/update")
    @SaCheckPermission(PERMISSION_UPDATE)
    public Result<Object> update(@RequestBody FlowProcessGroup flowProcessGroup) {
        ServiceCheckResult checkResult = processGroupService.checkAddAndUpdate(flowProcessGroup);
        if (checkResult.isResult()) {
            return Result.fail(checkResult.getMessage());
        }
        processGroupService.updateById(flowProcessGroup);
        return Result.successNoData();
    }

    @GetMapping("/delete")
    @SaCheckPermission(PERMISSION_DELETE)
    public Result<Object> delete(@RequestParam("processGroupId") Long processGroupId) {
        processGroupService.removeById(processGroupId);
        return Result.successNoData();
    }

    @GetMapping("/detail")
    @SaCheckPermission(PERMISSION_DETAIL)
    public Result<FlowProcessGroup> detail(@RequestParam("processGroupId") Long processGroupId) {
        return Result.successData(processGroupService.getById(processGroupId));
    }

    @GetMapping("/select")
    @SaCheckLogin
    public Result<List<SelectOption>> select() {
        return Result.successData(SelectOption.getSelectOptionList(processGroupService.list()));
    }
}
